﻿using System;
using System.Data.SqlClient;
using System.Windows.Forms;
using DataCenter.Interface;
using DataCenter.Interface.Model;

namespace DataCenter.Component.Processor
{
    public enum InterpolateTypes
    {
        Linear = 1,
        Switch = 2
    }

    [Processor("InterpolateProcessor", "1.0.0.1", "插值数据处理器，包括线性插值和状态插值")]
    public class InterpolateProcessorEntity : ComponentEntity
    {
        public InterpolateProcessorEntity()
        {
            base.ComponentType = ComponentTypes.Processor;
        }

        private bool isRealValueAsDefault = true;
        private InterpolateTypes interpolateType;
        private int forwardSearchMinutes;
        private int backwardSearchMinutes;

        [Parameter(DefaultValue = true)]
        public bool IsRealValueAsDefault
        {
            get { return isRealValueAsDefault; }
            set { isRealValueAsDefault = value; }
        }

        [Parameter(DefaultValue = InterpolateTypes.Linear)]
        public InterpolateTypes InterpolateType
        {
            get { return interpolateType; }
            set { interpolateType = value; }
        }

        [Parameter(DefaultValue = 0)]
        public int ForwardSearchMinutes
        {
            get { return forwardSearchMinutes; }
            set { forwardSearchMinutes = value; }
        }

        [Parameter(DefaultValue = 0)]
        public int BackwardSearchMinutes
        {
            get { return backwardSearchMinutes; }
            set { backwardSearchMinutes = value; }
        }

        public override bool Create(SqlCommand command)
        {
            InterpolateProcessorEditForm editForm = 
                new InterpolateProcessorEditForm(this, false);
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                ComponentConfig = ToXml().OuterXml;
                Insert(command);
                return true;
            }
            return false;
        }

        public override bool Modify(SqlCommand command)
        {
            InterpolateProcessorEditForm editForm =
                new InterpolateProcessorEditForm(this, true);
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                ComponentConfig = ToXml().OuterXml;
                Update(command);
                return true;
            }
            return false;
        }

        public override TComponent CreateComponent<TComponent>(SqlCommand command)
        {
            IProcessor component = new InterpolateProcessor(command, this);
            return (TComponent)component;
        }
    }
}
